package com.lu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.mapper.SysDeptMapper;
import com.lu.model.entity.SysDept;
import com.lu.model.entity.SysUser;
import com.lu.model.enums.YesOrNoEnum;
import com.lu.model.exception.LuBootAssert;
import com.lu.service.SysDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author wangqieyu
 * @since 2022-07-26
 */
@RequiredArgsConstructor
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

  @Override
  public Long addDept(SysDept dept) {
    LuBootAssert.isTrue(ObjectUtil.isAllNotEmpty(dept.getName()), "请输入组织名称");
    LuBootAssert.isTrue(ObjectUtil.isEmpty(dept.getStatus())
        || Arrays.stream(YesOrNoEnum.values()).anyMatch(o -> o.getValue() == dept.getStatus()), "无效的状态参数");
    this.checkNameUnique(null, dept.getName());
    SysUser loginUser = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    dept.setId(null);
    dept.setCreateUserId(loginUser.getId());
    dept.setUpdateUserId(loginUser.getId());
    this.baseMapper.insert(dept);
    return dept.getId();
  }

  @Override
  public SysDept updateDept(SysDept dept) {
    LuBootAssert.isTrue(ObjectUtil.isAllNotEmpty(dept.getId()), "无效的参数");
    LuBootAssert.isTrue(ObjectUtil.isEmpty(dept.getStatus())
        || Arrays.stream(YesOrNoEnum.values()).anyMatch(o -> o.getValue() == dept.getStatus()), "无效的状态参数");
    this.checkNameUnique(dept.getId(), dept.getName());
    SysUser loginUser = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    dept.setUpdateUserId(loginUser.getId());
    dept.setUpdateTime(LocalDateTime.now());
    this.baseMapper.updateById(dept);
    return this.baseMapper.selectById(dept.getId());
  }

  @Override
  public Integer deleteDept(Long id) {
    //删除部门
    int res = this.baseMapper.deleteById(id);
    return res;
  }

  @Override
  public List<SysDept.TreeNodes> getListByPid(Long pid, Integer status) {
    QueryWrapper<SysDept> deptQueryWrapper = new QueryWrapper<>();
    if(ObjectUtil.isNotEmpty(status)){
      deptQueryWrapper.eq("status", status);
    }
    if(ObjectUtil.isNotEmpty(pid)){
      deptQueryWrapper.eq("id", pid).or().eq("pid", pid);
    }
    deptQueryWrapper.orderByAsc("sort");
    List<SysDept> sysDepts = this.baseMapper.selectList(deptQueryWrapper);
    return SysDept.treeNodes(sysDepts, pid);
  }

  @Override
  public List<SysDept> sort(List<Long> ids) {
    if(ObjectUtil.isEmpty(ids)){
      return null;
    }
    for (int i = 0; i < ids.size(); i++) {
      SysDept dept = new SysDept();
      dept.setId(ids.get(i));
      dept.setSort(i);
      this.baseMapper.updateById(dept);
    }
    QueryWrapper<SysDept> deptQueryWrapper = new QueryWrapper<>();
    deptQueryWrapper.in("id", ids);
    return this.baseMapper.selectList(deptQueryWrapper);
  }

  private void checkNameUnique(Long id, String name){
    if(ObjectUtil.isEmpty(name)){
      return;
    }
    QueryWrapper<SysDept> deptQueryWrapper = new QueryWrapper<>();
    deptQueryWrapper.eq("name", name);
    if(ObjectUtil.isNotEmpty(id)){
      deptQueryWrapper.ne("id", id);
    }
    Integer count = this.baseMapper.selectCount(deptQueryWrapper);
    LuBootAssert.isTrue(count == 0, "组织名称不能重复");
  }

}
